package com.controller.yxy;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bean.yxy.Questionnaire;
import com.bean.yxy.Yxy0;
import com.service.yxyService.GetDataService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 问卷调查
 */
@Controller
@RequestMapping("/YXYpage/")
public class QuestionnaireController {
    @Autowired
    private HttpSession session;
    @Autowired
    private GetDataService  getDataService;

    private byte[] bytes;
    private String newfilename;
    /**
     *获取问卷所有信息
     * @param
     * @return
     */
    @RequestMapping(value = "getAllwenjuan",method = RequestMethod.GET)
    @ResponseBody
    public Object getAllwenjuan(){
               getDataService.timejiaodui();
        return getDataService.getAllwenjuan();
    }

    /**
     * 获取题目
     * @param vid
     * @return
     */
    @RequestMapping(value = "xiangxixinxi",method = RequestMethod.GET)
    @ResponseBody
    public Object xiangxixinxi(String vid){
        return getDataService.xiangxixinxi(vid);
    }

    /**
     * 获取对于题目的填写答案
     * @param vid
     * @param sid
     * @return
     */
    @RequestMapping(value = "getdaan",method = RequestMethod.GET)
    @ResponseBody
    public Object getdaan(String vid,String sid){
        return getDataService.getdaan(vid,sid);
    }
    /**
     * 获取对于题目的选择答案
     * @param vid
     * @param sid
     * @return
     */
    @RequestMapping(value = "getdaan1",method = RequestMethod.GET)
    @ResponseBody
    public Object getdaan1(String vid,String sid){
        return getDataService.getdaan1(vid,sid);
    }

    /**
     * 添加答案
     * @param
     * @return
     */
    @RequestMapping(value = "adddaan",method = RequestMethod.POST)
    @ResponseBody
    public Object adddaan(String str){
       JSONArray o = JSON.parseArray(str);
       List<Questionnaire> lis = o.toList(Questionnaire.class);
        return getDataService.adddaan(lis);
    }


    /**
     *根据题目类型加载数据
     * @param sid
     * @return
     */
    @RequestMapping(value = "gettimuleixing",method = RequestMethod.POST)
    @ResponseBody
    public Object gettimuleixing(String sid){
        return getDataService.gettimuleixing(sid);
    }


    /**
     * 添加题目
     * @param timu
     * @return
     */
    @RequestMapping(value = "addtimu",method = RequestMethod.POST)
    @ResponseBody
    public Object addtimu(String timu,String vid,String lx){
        return getDataService.addtimu(timu,vid,lx);
    }

    /**
     * 自动搜索标题
     * @param
     * @return
     */
    @RequestMapping(value = "chaxunbiaotiba",method = RequestMethod.GET)
    @ResponseBody
    public Object chaxunbiaotiba(String biaotimc){
        return getDataService.chaxunbiaotiba(biaotimc);
    }

    /**
     * 删除题目
     * @param sid
     * @return
     */
    @RequestMapping(value = "delecttimu",method = RequestMethod.POST)
    @ResponseBody
    public Object delecttimu(String sid){
        return getDataService.delecttimu(sid);
    }

    /**
     * 删除填写答案
     */
    @RequestMapping(value = "delectdaan",method = RequestMethod.POST)
    @ResponseBody
    public Object delectdaan(String daid){
        return getDataService.delectdaan(daid);
    }

    /**
     * 删除选择答案
     */
    @RequestMapping(value = "delectdaan1",method = RequestMethod.POST)
    @ResponseBody
    public Object delectdaan1(String daid){
        return getDataService.delectdaan1(daid);
    }

    @RequestMapping(value = "test",method = RequestMethod.POST)
    @ResponseBody
    public Map s(@RequestParam("file") MultipartFile mf){
        Map<String,Object> m = new HashMap<>();
        String uploadFileName = mf.getOriginalFilename();
        String[]  os=uploadFileName.split("\\.");
         newfilename = getsj()+"."+os[1];
        try {
            if(mf.getOriginalFilename()==null){
                m.put("mag","false");
                return m;
            }
            bytes= mf.getBytes();
            m.put("mag","true");
            return m;
        } catch (IOException e) {
            e.printStackTrace();
        }
        m.put("mag","false");
        return m;
    }

    /**
     * 添加问卷内容/文件上传
     * @param
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "uploadFile0",method = RequestMethod.POST)
    @ResponseBody
    public Object uploadFile(Yxy0 y) {
        try {
        String url1 = session.getServletContext().getRealPath("/");
        String url2=url1+"\\img\\"+newfilename;
            File newFile1 = new File(url2);
            if(newFile1.exists()){
                newFile1.createNewFile();
            }
            FileOutputStream outputStream = new FileOutputStream(newFile1);
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
           int i = getDataService.uploadFile(y,newfilename);
           if(i==1){
            return true;
           }else if(i==-1){
            return false;
           }else {
            return false;
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
          return null;
    }

    /**
     * 获取当前时间搓
     * @return
     */
    public long getsj(){
       return System.currentTimeMillis();
    }

    /**
     * 获取指定时间错
     * @return
     */
    public long getzhidingsj(String sj){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = df.parse(sj);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        long timestamp = cal.getTimeInMillis();
        return timestamp;
    }

    /**
     * 删除标题
     * @param request
     * @return
     */
    @RequestMapping(value = "delectbiaoti",method = RequestMethod.POST)
    @ResponseBody
    public Object delectbiaoti(HttpServletRequest request){
       String[] str =  request.getParameterValues("ar[]");
       return getDataService.delectbiaoti(str);
    }

    /**
     * 新获取所有题目
     * @param vid
     * @return
     */
    @RequestMapping(value = "gettimeByvid",method = RequestMethod.GET)
    @ResponseBody
    public Object  gettimeByvid(String vid){
        return  getDataService.gettimeByvid(vid);
    }






    /**
     * 是否投票 获取所有正在进行中的
     * @return
     */
    @RequestMapping(value = "istoupiao",method = RequestMethod.GET)
    @ResponseBody
    public Object istoupiao(String vid){
        return getDataService.istoupiao(vid);
    }

    /**
     * 投票
     * @param fig 投票状态
     * @return
     */
    @RequestMapping(value = "yonghutoupiao",method = RequestMethod.GET)
    @ResponseBody
    public Object yonghutoupiao(String fig,String tpid){
        return getDataService.yonghutoupiao(fig,tpid);
    }


    /**
     * 判断是否是住户
     * @return
     */
    @RequestMapping(value = "iszhuhu",method = RequestMethod.GET)
    @ResponseBody
    public Object iszhuhu(){
        Map<String,Object> map = (Map<String,Object>) session.getAttribute("user");
        Integer id = (Integer) map.get("eid");
        System.out.println(id);
        if(id==null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 投票
     * @param
     * @return
     */
    @RequestMapping(value = "gettabletoupiaolist",method = RequestMethod.GET)
    @ResponseBody
    public Object gettabletoupiaolist(String sid){
        return  getDataService.gettabletoupiaolist(sid);
    }
    /**
     * poll获取投票详情
     * @param sid
     * @return
     */
    @RequestMapping(value = "gettoupiao",method = RequestMethod.POST)
    @ResponseBody
    public Object gettoupiao(String sid){
        return  getDataService.gettoupiao(sid);
    }


}
